/*
 * @Descripttion:
 * @Author: xiaodong
 * @Date: 2020-09-07 16:17:39
 */

const path = require("path");
const DistInitPlugin = require("cloud-common/config/plugin/DistInitPlugin.js");
const DistEndPlugin = require("cloud-common/config/plugin/DistEndPlugin.js");
const FailedPlugin = require("cloud-common/config/plugin/FailedPlugin.js");
const DonePlugin = require("cloud-common/config/plugin/DonePlugin.js");
const devServer = require("cloud-common/config/devServer.js");
const urlConfig = require("./config/common/urlConfig.js");
const version = new Date().getTime();
const {
    name,
    port
} = require("./package");
const publicPath = process.env.PACK_CDN
  ? urlConfig[process.env.mode]["CDN_URL"]
  : "./";
function resolve(dir) {
    return path.join(__dirname, dir);
}
module.exports = {
    devServer: devServer,
    lintOnSave: false,
    outputDir: "dist",
    productionSourceMap: false, // map 映射
    publicPath: publicPath,
    chainWebpack: config => {
        config.plugin("define").tap(args => {
            args[0].URL_CONFIG = JSON.stringify(urlConfig[process.env.mode]);
            return args;
        });
        config.module
            .rule("fonts")
            .use("url-loader")
            .loader("url-loader")
            .options({
                limit: 4096, // 小于4kb将会被打包成 base64
                fallback: {
                    loader: "file-loader",
                    options: {
                        name: "fonts/[name].[hash:8].[ext]",
                        publicPath,
                    },
                },
            })
            .end();
        config.module
            .rule("images")
            .use("url-loader")
            .loader("url-loader")
            .options({
                limit: 4096, // 小于4kb将会被打包成 base64
                fallback: {
                    loader: "file-loader",
                    options: {
                        name: "img/[name].[hash:8].[ext]",
                        publicPath,
                    },
                },
            });
    },
    // 自定义webpack配置
    configureWebpack: {
        plugins: [
            new DistInitPlugin(),
            new DistEndPlugin(),
            new FailedPlugin(),
            new DonePlugin(),
        ],
        resolve: {
            alias: {
                "@": resolve("src"),
            },
        },
        output: {
            // 把子应用打包成 umd 库格式
            library: `${name}-[name]`,
            libraryTarget: "umd",
            jsonpFunction: `webpackJsonp_${name}`,
            filename: `static/js/[name]_[hash]_${version}.js?`,
            chunkFilename: `static/js/[name]_[hash]_${version}.js`,
        },
        externals: {
            // vue: "Vue",
            // "vue-router": "VueRouter",
            // vuex: "Vuex",
        },
    },
    css: {
        loaderOptions: {
            less: {
                lessOptions: {
                  javascriptEnabled: true,
                  math: 'always'   // 此处指定为兼容 less-loader 3.x 的默认选项
                }
              }
        },
    },
    transpileDependencies: [
        "single-spa",
        "qiankun",
        "import-html-entry",
        "cloud-components",
        "cloud-common",
        "ant-design-vue",
    ],
};